package fhb.mobile.todoapp;

import java.util.ArrayList;
import java.util.List;

import fhb.mobile.todoapp.datamodell.Contact;
import fhb.mobile.todoapp.datamodell.ToDoItem;
import fhb.mobile.todoapp.datamodell.ToDoItemList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class ChangeAssociatedContactsActivity extends Activity {
	
	private ListView contactList = null;
	private Button saveButton = null;

	private ToDoItem modifiedToDoItem = null;
	private List<Contact> contacts;
	
	@Override
	public void onBackPressed() {
		startActivity(new Intent(ChangeAssociatedContactsActivity.this, EditToDoItemActivity.class));
		
		super.onBackPressed();
	}
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.todoliste_change_associated_contacts);
        
        modifiedToDoItem = ToDoItemList.getModifiedToDoItem();
        
        contacts = getContacts();
        ArrayAdapter<Contact> adapter = new ContactAdapter(this, R.layout.todoliste_change_associated_contacts_item, contacts);
        contactList = (ListView) findViewById(R.id.todoliste_change_associated_contacts_listView_contacts);
        contactList.setAdapter(adapter);
        saveButton = (Button) findViewById(R.id.todoliste_change_associated_contacts_button_save);
                
        saveButton.setOnClickListener(
        		new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(ChangeAssociatedContactsActivity.this, EditToDoItemActivity.class);
						modifiedToDoItem.getAssociatedContacts().clear();
						for (Contact contact : contacts) {
							if (contact.isChecked()){
								modifiedToDoItem.addAssociatedContact(contact.getId());
							}
						}
						
						startActivity(intent);
						finish();
					}
				});
    }
    
    private class ContactAdapter extends ArrayAdapter<Contact> {
    	
    	private List<Contact> contacts;

		public ContactAdapter(Context context, int textViewResourceId, List<Contact> contacts) {
			super(context, textViewResourceId, contacts);
			this.contacts=contacts;
		}
    	
		public View getView(final int position, View listItemView, ViewGroup parent){
			LayoutInflater inflater = (LayoutInflater) ChangeAssociatedContactsActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View v = inflater.inflate(R.layout.todoliste_change_associated_contacts_item, null);
			final Contact item = contacts.get(position);
			
			final ImageButton checked = (ImageButton) v.findViewById(R.id.todoliste_change_associated_contacts_item_imageButton_checked);
			TextView name = (TextView) v.findViewById(R.id.todoliste_change_associated_contacts_item_textView_contactName);
			ImageButton send = (ImageButton) v.findViewById(R.id.todoliste_change_associated_contacts_item_imageButton_sendMail); 
			if (item.isChecked()){
				checked.setImageResource(android.R.drawable.checkbox_on_background);
			} else {
				checked.setImageResource(android.R.drawable.checkbox_off_background);
			}
			
			//name.setText(item.getFieldValue(Contact.FIELD_NAME).toString());
			name.setText(item.getName());
			
			send.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(android.content.Intent.ACTION_SEND);
					intent.setType("plain/text");
					
					//intent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] {item.getFieldValue(Contact.FIELD_EMAILS).toString(), "tino@sterneundmon.de"});
					intent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] {item.getMail(), "tino@sterneundmon.de"});
					intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "FROM 2WUNDERKINDER 2.0: " + modifiedToDoItem.getTitle());
					intent.putExtra(android.content.Intent.EXTRA_TEXT, modifiedToDoItem.getContent());
					
					startActivity(Intent.createChooser(intent, "Send your mail in:"));
					
					
				}
			});
			
			checked.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					item.setChecked(!item.isChecked());
					if (item.isChecked()){
						checked.setImageResource(android.R.drawable.checkbox_on_background);
					} else {
						checked.setImageResource(android.R.drawable.checkbox_off_background);
					}
					checked.invalidate();
				}
			});
			
			return v;
		}
		
    }
    
    protected List<Contact> getContacts(){
    	 List<Contact> contacts = new ArrayList<Contact>();
    	 Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    	 
    	 while(cursor.moveToNext()){
    		 String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    		 Contact currentContact = new Contact();
    		 
    		 currentContact.setId(Integer.parseInt(contactId));
    		 currentContact.setName(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
    		 System.out.println("3");
    		 currentContact.setChecked(modifiedToDoItem.isRelatedContact(Integer.parseInt(contactId)));
    		 System.out.println("2");
    		 //currentContact.putFieldValue(Contact.FIELD_NAME, cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
    		 
    		 contacts.add(currentContact);
    		 
    		 Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
    		 while(emails.moveToNext()){
    			 String emailadress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA1));
    			 //currentContact.addEmailAddress(emailadress);
    			 currentContact.setMail(emailadress);
    			 System.out.println(emailadress);
    		 }
    		 emails.close();
    	 }
    	 cursor.close();
    	 
    	 
    	 
    	 return contacts; 
    }
   
}
